/* version_date ("models")("")
 * error list ("models", "model") ("date", "name")
 * error desc ("models", "model", "error")("data", "modelname", "error no")
 * 
 * <models 10.12.2012>
	<model>360vX_YYN
		<error>1
			<descritpion>OCT is disconnected</descritpion>
		</error>
		<error>2

 *
 */
 





package XMLReader;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import res.xml.*;
/**
 *
 * @author moti
 */
public class Driver
{
	List <String>models;
	String [] errors;
	XMLTree tree;
	public Driver()
	{
		models= new ArrayList<String>();
		errors = new String[32];
		for(int i=0; i<32; i++)
			errors[i]="";
	}
	
	public String[] firmwareValues() 
	{
		String [] values=  new String[ models.size()];
		models.toArray(values);
        return values;
	}
	
    private static final XMLParser parser = new XMLParser();
 
    
    public boolean setFirmwareValues(List<String> lines)
    {
    	/*for(String t:lines)
    		System.out.println(t);*/
    	for(int i=0; i<lines.size();i++)
		{
			//String k=lines.get(i);
			parser.parse(lines.get(i));
		}
		tree = parser.getTree();
		String[] tags = new String[] {"models"};//, "model", "errors"};
        String[] vals = new String[] {""}; //, "360vX_YYN", ""};
        XMLEntry entry = tree.find(tags, vals);
        if (entry != null) 
        {
            String[][] res = tree.get(entry);
            try 
            {
                for (int i = 0;i<res.length; i++) 
                {
                    if (!res[i][0].isEmpty()) 
                    {
                        if(!res[i][1].isEmpty())
                        		models.add(res[i][1]);
                    }
                }
                
            } 
            catch(Exception e) 
            {
                System.out.println("set models error");
                return false;
            }
            
        } 
        else 
        	return false;
        
        return true;
        
    }
    
    //get descriptions values
    public boolean setErrors(List<String> lines, String firmwareValue)
    {
    	//reset errors list
    	for(int i=0; i<errors.length;i++)
    		errors[i]="";
        for(int errorNum=1; errorNum<32; errorNum++)
        {
        	String[] tags = new String[] {"models","model","error"};
	        String[] vals = new String[] {"",firmwareValue,String.valueOf(errorNum)};
	        XMLEntry entry = tree.find(tags, vals);
	        if (entry != null) 
	        {
	            String[][] res = tree.get(entry);
	            try 
	            {
	                for (int i = 0;i<res.length; i++) 
	                {
	                    if (!res[i][0].isEmpty()) 
	                    {
	                        if(!res[i][1].isEmpty())
	                        	if(res[i][0].contains("description"))//need change to description
	                        		errors[errorNum-1]=res[i][1];	//models.add(res[i][1]);*/
	                    }
	                }
	                
	            } 
	            catch(Exception e) 
	            {
	                System.out.println("error");
	                return false;
	            }
	            
	        } 
	        else
	        {
	        	if(errors[0]!="")
	        		continue;
	        	else
	        	return false;
	        }
        }
        
        return true;
    }

    //return errors DescriptionList
    public String[] errorsDescription() 
    {
		return errors;
	}
}

